const app = getApp()
Page({
  data: {
    inputText: 'Hello World!',
    receiveText: '',
    name: '',
    connectedDeviceId: '',
    services: {},
    characteristics: {},
    connected: true,
    gbuff:[1,1,1]
  },
  bindInput: function (e) {
    this.setData({
      inputText: e.detail.value
    })
    console.log(e.detail.value)
  },
  Send: function () {
    var that = this
    if (that.data.connected) {
      var buffer = new ArrayBuffer(that.data.inputText.length)
      var dataView = new Uint8Array(buffer)
      for (var i = 0; i < that.data.inputText.length; i++) {
        // 字符串转化为Unicode
        dataView[i] = that.data.inputText.charCodeAt(i)
      }

      wx.writeBLECharacteristicValue({
        deviceId: that.data.connectedDeviceId,
        serviceId: that.data.services[0].uuid,
        characteristicId: that.data.characteristics[0].uuid,
        value: buffer,
        success: function (res) {
          console.log('发送成功')
        }
      })
    }
    else {
      wx.showModal({
        title: '提示',
        content: '蓝牙已断开',
        showCancel: false,
        success: function (res) {
          that.setData({
            searching: false
          })
        }
      })
    }
  },
  onLoad: function (options) {
    var that = this
    console.log(options)
    that.setData({
      name: options.name,
      connectedDeviceId: options.connectedDeviceId
    })
    wx.getBLEDeviceServices({
      deviceId: that.data.connectedDeviceId,
      success: function (res) {
        console.log('getBLEDeviceServices',res.services)
        for (let i = 0; i < res.services.length; i++) {
          console.log(res.services[i].uuid);
        }
        that.setData({
          services: res.services
        })
        wx.getBLEDeviceCharacteristics({
          deviceId: options.connectedDeviceId,
          serviceId: res.services[0].uuid,
          success: function (res) {
            console.log('getBLEDeviceCharacteristics')
            console.log(res.characteristics)
            that.setData({
              characteristics: res.characteristics
            })
            for (let j = 0; j < res.characteristics.length; j++) {
              let item = res.characteristics[j]
      
              if (item.properties.indicate) {
              //可读数据
                wx.notifyBLECharacteristicValueChange({
                  state: true,
                  deviceId: options.connectedDeviceId,
                  serviceId: that.data.services[0].uuid,
                  characteristicId: item.uuid,
                  success: function (res) {
                    console.log('启用notify成功')
                  }
                })
              }
            }
          }
        })
      }
    })
    wx.onBLEConnectionStateChange(function (res) {
      console.log(res.connected)
      that.setData({
        connected: res.connected
      })
    })
    wx.onBLECharacteristicValueChange(function (res) {
      var receiveText = app.buf2string(res.value)
      console.log('接收到数据：' + receiveText)
      that.setData({
        receiveText: receiveText
      })
    })
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onLed1off:function(){
    var that = this
    if (that.data.connected) {
      var buffer = new ArrayBuffer(3)  
      var dataView = new Uint8Array(buffer) 
      that.data.gbuff[0] = 1
      dataView[0]  = that.data.gbuff[0];
      dataView[1]= that.data.gbuff[1];
      dataView[2] = that.data.gbuff[2];
      console.log('发送内容'+dataView[0]+dataView[1]+dataView[2]);
      that.sendCmd(buffer);
    }
  },  
  onLed2off:function(){
    var that = this
    if (that.data.connected) {
      var buffer = new ArrayBuffer(3)  
      var dataView = new Uint8Array(buffer) 
      that.data.gbuff[1] = 1
      dataView[0]  = that.data.gbuff[0];
      dataView[1]= that.data.gbuff[1];
      dataView[2] = that.data.gbuff[2];
      console.log('发送内容'+dataView[0]+dataView[1]+dataView[2]);
      that.sendCmd(buffer);
    }
  },
  onLed3off:function(){
    var that = this
    if (that.data.connected) {
      var buffer = new ArrayBuffer(3)  
      var dataView = new Uint8Array(buffer) 
      that.data.gbuff[2] = 1
      dataView[0]  = that.data.gbuff[0];
      dataView[1]= that.data.gbuff[1];
      dataView[2] = that.data.gbuff[2];
      console.log('发送内容'+dataView[0]+dataView[1]+dataView[2]);
      that.sendCmd(buffer);
    }
  },
  onLed1on:function(){
    var that = this
    if (that.data.connected) {
      var buffer = new ArrayBuffer(3)  
      var dataView = new Uint8Array(buffer) 
      
      that.data.gbuff[0] = 0
      dataView[0]  = that.data.gbuff[0];
      dataView[1]= that.data.gbuff[1];
      dataView[2] = that.data.gbuff[2];
      console.log('发送内容'+dataView[0]+dataView[1]+dataView[2]);
      that.sendCmd(buffer);
    }    
  },
  onLed2on:function(){
    var that = this
    if (that.data.connected) {
      var buffer = new ArrayBuffer(3)  
      var dataView = new Uint8Array(buffer) 
      that.data.gbuff[1] = 0
      dataView[0]  = that.data.gbuff[0];
      dataView[1]= that.data.gbuff[1];
      dataView[2] = that.data.gbuff[2];
      console.log('发送内容'+dataView[0]+dataView[1]+dataView[2]);
      that.sendCmd(buffer);
    }
  },
  onLed3on:function(){
    var that = this
    if (that.data.connected) {
      var buffer = new ArrayBuffer(3)  
      var dataView = new Uint8Array(buffer) 
      that.data.gbuff[2] = 0
      dataView[0]  = that.data.gbuff[0];
      dataView[1]= that.data.gbuff[1];
      dataView[2] = that.data.gbuff[2];
      console.log('发送内容'+dataView[0]+dataView[1]+dataView[2]);
      that.sendCmd(buffer);
    }
  },
  sendCmd:function(buff){
    var that = this
    if (that.data.connected) {
      wx.writeBLECharacteristicValue({
        deviceId: that.data.connectedDeviceId,
        serviceId: that.data.services[0].uuid,
        characteristicId: that.data.characteristics[0].uuid,
        value: buff,
        success: function (res) {
          console.log('发送成功')
        }
      })
    }
    else {
      wx.showModal({
        title: '提示',
        content: '蓝牙已断开',
        showCancel: false,
        success: function (res) {
          that.setData({
            searching: false
          })
        }
      })
    }
  }

})